package ac.manchester.cs.afzal.autoservd.pattern_extraction;

import ac.manchester.cs.afzal.autoservd.file_handler.File_Manager;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.Properties;
import java.util.Vector;

public class Process_Dependency_Parse
{

    Vector list_dirs, local_sen_ids, occurrence_values, sen_ids;
    Vector[] dependency_statements;
    private String DEP_FILES_LOCATION = "DEP_FILES_LOCATION";
    private String DEP_STATEMENTS_LOCATION = "DEP_STATEMENTS_LOCATION";
    
    String[] Class =
    {
        "algorithms", "alignments", "analysis", "applications", "approaches",
        "data", "databases", "datasets", "files", "libraries", "methods",
        "models",
        "procedures", "programs", "records", "reports", "repositories",
        "resources",
        "sequences", "searches", "services", "softwares", "systems", "tools",
        "trees"
    };
    File file_doc;
    static private SQL_Handler_PE sqlHandler;
    private File_Manager File_Manager;
    private Properties props;

    public Process_Dependency_Parse()
    {
        File_Manager = new File_Manager();
        sqlHandler = new SQL_Handler_PE();
        sqlHandler.connect_db();

        props = new Properties();
        try
        {
            props.load(new FileInputStream("PE.properties"));
        }
        catch (Exception ex)
        {
            System.out.println("Exception while loading propoerties");
        }
        DEP_FILES_LOCATION = props.getProperty(DEP_FILES_LOCATION);
        DEP_STATEMENTS_LOCATION = props.getProperty(DEP_STATEMENTS_LOCATION);
    }

        /* Running time almost 5 hours*/
    public void dep_statement_extract()
    {
        StringBuffer strBuf;

        for (int k = 0; k < Class.length; k++)
        {
            strBuf = new StringBuffer();

            System.out.println("Processing Starts for Class : " + Class[k]);

            list_dirs = sqlHandler.get_sentences_per_semantic_type(Class[k]);
            local_sen_ids = sqlHandler.local_sen_ids;
            occurrence_values = sqlHandler.occurrence_values;
            sen_ids = sqlHandler.sen_ids;

            dependency_statements = new Vector[list_dirs.size()];

            for (int i = 0; i < list_dirs.size(); i++)
            {
                dependency_statements[i] = new Vector();
                if (Integer.parseInt(local_sen_ids.elementAt(i).toString()) < 10)
                {
                    file_doc = new File(DEP_FILES_LOCATION + list_dirs.elementAt(i) + "//00" + local_sen_ids.elementAt(i) + "__" +
                            occurrence_values.elementAt(i) + ".txt.stp");
                }
                else if (Integer.parseInt(local_sen_ids.elementAt(i).toString()) < 100)
                {
                    file_doc = new File(DEP_FILES_LOCATION + list_dirs.elementAt(i) + "//0" + local_sen_ids.elementAt(i) + "__" +
                            occurrence_values.elementAt(i) + ".txt.stp");
                }
                else
                {
                    file_doc = new File(DEP_FILES_LOCATION + list_dirs.elementAt(i) + "//" + local_sen_ids.elementAt(i) + "__" +
                            occurrence_values.elementAt(i) + ".txt.stp");
                }

                try
                {
                    BufferedReader in = new BufferedReader(new FileReader(file_doc));
                    String str;
                    while ((str = in.readLine()) != null)
                    {
                        dependency_statements[i].add(str);
                    }
                    in.close();
                }
                catch (IOException e)
                {
                    System.out.println("Error in reading file 1 " + e.getMessage());
                }

                Iterator vectorIterator = dependency_statements[i].iterator();
                while (vectorIterator.hasNext())
                {
                    String statement = vectorIterator.next().toString();
                    if ((statement.contains("dobj")) && (statement.contains("Main_")))
                    {
                        strBuf.append(statement + " : Doc : " + list_dirs.elementAt(i) + " and SenID : " + local_sen_ids.elementAt(i));
                        strBuf.append(System.getProperty("line.separator"));
                    }
                    else if ((statement.contains("nsubjpass")) && (statement.contains("Main_")))
                    {
                        strBuf.append(statement + " : Doc : " + list_dirs.elementAt(i) + " and SenID : " + local_sen_ids.elementAt(i));
                        strBuf.append(System.getProperty("line.separator"));
                    }
                    else if ((statement.contains("dobj")) && (statement.contains("Main_")))
                    {
                        strBuf.append(statement + " : Doc : " + list_dirs.elementAt(i) + " and SenID : " + local_sen_ids.elementAt(i));
                        strBuf.append(System.getProperty("line.separator"));
                    }
                }
            }
            File_Manager.fileWriter(strBuf.toString(), DEP_STATEMENTS_LOCATION + Class[k] + ".txt");
        }
    }

    /* Running time less than a minute*/
    public void fill_the_predicates_table()
    {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String input;
        try
        {
            System.out.println("*********************************************");
            System.out.print(
                    "Please Choose among following options:\n" +
                    " 1. Do you want to create new predicates table ?  \n" +
                    " 2. Do you want to delete previous predicate entries \n" +
                    " 3. Otherwise \n" +
                    " Enter number to continue or 'e' to exit : ");
            input = in.readLine();
            input = input.trim();

            if (input.equals("1"))
            {
                sqlHandler.create_predicate_table();
            }
            else if (input.equals("2"))
            {
                sqlHandler.delete_all(new String[]
                        {
                            "__predicates"
                        });
                sqlHandler.reset_auto_inc(new String[]
                        {
                            "__predicates"
                        });
            }
            else if (input.equals("e"))
            {
                System.exit(1);
            }
        }
        catch (Exception ex)
        {
            System.out.println("SQL Exception: Reading input: " + ex.getMessage());
        }

        for (int k = 0; k < Class.length; k++)
        {
            int index_predicate_start = 0;
            String doc_value = "";
            String sentence_id = "";
            String predicate = "";
            try
            {
                BufferedReader in1 = new BufferedReader(new FileReader(DEP_STATEMENTS_LOCATION + Class[k] + ".txt"));

                String str;
                while ((str = in1.readLine()) != null)
                {
                    if ((index_predicate_start = str.indexOf("dobj(", 0)) != -1)
                    {
                        index_predicate_start += 5;
                        int temp = str.indexOf("-", index_predicate_start);
                        predicate = str.substring(index_predicate_start, temp);

                        if (predicate.equals("Main"))
                        {
                            index_predicate_start = str.indexOf(" ", temp);
                            index_predicate_start++;
                            temp = str.indexOf("-", index_predicate_start);
                            predicate = str.substring(index_predicate_start, temp);
                        }

                        int temp_a, temp_b;
                        temp_a = str.indexOf("Doc : ");
                        temp_a += 6;
                        temp_b = str.indexOf(" ", temp_a);
                        doc_value = str.substring(temp_a, temp_b);

                        temp_a = str.indexOf("SenID : ");
                        temp_a += 7;
                        temp_b = str.indexOf(" ", temp_a);
                        sentence_id = str.substring(temp_a, str.length());

                        if ((predicate.length() > 1) && !(predicate.contains("Term")))
                        {
                            String[] doc_table1 =
                            {
                                "predicate", "Sen_ID", "Doc_Value", "Class",
                                "dep"
                            };
                            String[] doc_table_value1 =
                            {
                                predicate, sentence_id, doc_value, Class[k],
                                "DOBJ",
                            };
                            sqlHandler.insert_table("__predicates", doc_table1, doc_table_value1);
                        }
                    }
                }
                in.close();
            }
            catch (IOException e)
            {
                System.out.println("Error in reading file : " + e.getMessage());
            }
        }
    }
}
/* "algorithms", "alignments", "analysis", "applications", "approaches",
"data", "databases", "datasets", "files", "libraries", "methods",
"models",
"procedures", "programs", "records", "reports", "repositories",
"resources",
"sequences", "searches", "services", "softwares", "systems", "tools",
"trees"*/
